import type { IUser, PermissionValues } from '@/types';
import type { FilterDocType, TabViewType,ListViewData } from '@/types/common';
import {defineStore,acceptHMRUpdate} from 'pinia'
import {ref} from 'vue'
 
/**
 * 当前登录用户信息
 */
export const useUserStore = defineStore('userStore', ()=>{
    const name = ref('')
    const staffId = ref('')
    const user = ref<IUser>()
    

    function login(loginUser:IUser){
        if(loginUser.id){
            staffId.value =  loginUser.id;
        }

        user.value = loginUser
        
    }

    function hasPermission(perm:PermissionValues){
        var result = false;

        user.value?.perms?.forEach((item)=>{
            if(item == perm ){
                result = true;
            }
        })

        return result;
    }

    return {
        name,staffId,login,user,hasPermission
    }

});

export const useParamStore = defineStore('paramStore',()=>{
    const redirectURL = ref('');
    const redirectURI = ref('');
    
    function setRedirectURL(url:string){
        redirectURL.value = url;
    }

    function setRedirectURI(uri:string){
        redirectURI.value = uri;
    }

    return {
        redirectURL,setRedirectURL,
        redirectURI,setRedirectURI
    }
});

export const useSearchStore = defineStore('searchStore',()=>{
    const key = ref('')

    function setKey  (val?:string){
        console.log('set key='+val)
        if(val){
            key.value = val;
        }else{
            key.value = ''
        }
        
    }

    return {
        key,setKey
    }
});

/**
 * 各类公文的数量
 */
export const useDocTypeCountStore = defineStore('docTypeCount',()=>{
    const allCount = ref<number|string>(0)
    const sendCount = ref<number|string>(0)
    const receiveCount = ref<number|string>(0)
    const reportCount = ref<number|string>(0)    
    const represnetCount = ref<number|string>(0)
    const askLeaveCount = ref<number|string>(0)
    const sealCount = ref<number|string>(0)

    const setCount = (all:number|string,send:number|string,receive:number|string,report:number|string,
        ask:number|string,seal:number|string,represent:number|string)=>{
        allCount.value = all;
        sendCount.value = send;
        receiveCount.value = receive;
        reportCount.value = report;
        askLeaveCount.value = ask;
        sealCount.value = seal;
        represnetCount.value = represent;
    }

    return {
        allCount,sendCount,receiveCount,reportCount,represnetCount,askLeaveCount,sealCount,
        setCount
    }
})


 
/**
 *  记录哪个tab页面被选中
 */
export const useTabViewStore = defineStore('tabViewStore',()=>{
    
    const currentView = ref<TabViewType>({todo:true});
    const currentFilter = ref<FilterDocType>({all:true});
 
    const setCurrentView = (val:TabViewType)=>{
        currentView.value = val;
    }

    const setCurrentFilter = (val?:FilterDocType)=>{
        if(val){
            currentFilter.value = val;
        }
    }

    return {
        currentView,setCurrentView,currentFilter,setCurrentFilter
    }
})


/**
 *  当前页面状态
 */
export const useListViewDataStore = defineStore('listViewDataStore',()=>{
    
    const currentView = ref<TabViewType>({todo:true});//哪个tab被选中

    const todo = ref<ListViewData>({});
    const toRead = ref<ListViewData>({});
    const redirect = ref<ListViewData>({});
    const done = ref<ListViewData>({});
    const favo = ref<ListViewData>({});

    return {
        todo,toRead,redirect,done,favo,currentView
    }
    
})


export const userSchoolLeaderStore = defineStore('userschoolLeaderStore',()=>{
    const leaderList = ref<string[]>([]);
    
    
    function setLeaders(list:string[]){
        leaderList.value = list;
    }

    
    return {
        leaderList,
        setLeaders
    }
});

if(import.meta.hot){
    import.meta.hot.accept(acceptHMRUpdate(useUserStore,import.meta.hot));
}
